package cn.edu.buaa.sei.jdat.vt;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.buaa.sei.jdat.JarController.ProgressMonitor;
import cn.edu.buaa.sei.jdat.exception.InvalidURIException;
import cn.edu.buaa.sei.jdat.vt.widgets.JarListView.Item;

import com.cocotingo.snail.dispatcher.Dispatcher;

// TODO: implement import dialog
public class ImportDialog extends JDialog {

	private static final long serialVersionUID = 23976572080766071L;
	private final JPanel contentPanel = new JPanel();
	private final MainView mainView;

	/**
	 * Create the dialog.
	 */
	public ImportDialog(MainView mainView) {
		super();
		this.mainView = mainView;
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JButton btnImportSampleJar = new JButton("import sample jars");
			btnImportSampleJar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					// import sample jars
					final String[] urls = {
							"lib/snail-gui-5.jar",
							"Sample Jars/org.eclipse.text.jar",
							"Sample Jars/org.eclipse.ui.browser.jar",
							"Sample Jars/org.eclipse.ui.console.jar",
							"Sample Jars/org.eclipse.ui.editors.jar",
							"Sample Jars/org.eclipse.ui.externaltools.jar",
							"Sample Jars/org.eclipse.ui.forms.jar",
							"Sample Jars/org.eclipse.ui.ide.application.jar",
							"Sample Jars/org.eclipse.ui.jar",
							"Sample Jars/org.eclipse.ui.navigator.jar",
							"Sample Jars/org.eclipse.ui.net.jar",
							"Sample Jars/org.eclipse.ui.presentations.jar",
							"Sample Jars/org.eclipse.ui.views.jar",
							"Sample Jars/org.eclipse.ui.views.log.jar",
					};
					
					for (int i = 0; i < urls.length; i++) {
						importSingleJar(urls[i]);
					}
					
					dispose();
				}
			});
			contentPanel.add(btnImportSampleJar);
		}
		{
			JButton btnImportSampleUrl = new JButton("import sample url jar");
			btnImportSampleUrl.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					// import sample url jar
					importSingleJar("http://jdat.googlecode.com/files/snail-gui-5.jar");
					dispose();
				}
			});
			contentPanel.add(btnImportSampleUrl);
		}
		{
			JButton btnDoNotClick = new JButton("do not click");
			btnDoNotClick.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					// /Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/
					final String[] urls = {
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/lib/snail-gui-5.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.text.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.browser.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.console.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.editors.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.externaltools.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.forms.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.ide.application.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.navigator.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.net.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.presentations.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.views.jar",
							"/Users/gong/Documents/Eclipse Workspace/JDAT Visual Tool/Sample Jars/org.eclipse.ui.views.log.jar",
					};
					
					for (int i = 0; i < urls.length; i++) {
						importSingleJar(urls[i]);
					}
					
					dispose();
				}
			});
			contentPanel.add(btnDoNotClick);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	private void importSingleJar(final String url) {
		try {
			mainView.getJarController().importJar(url, new ProgressMonitor() {
				@Override
				public void updateProgress(final double progress) {
					Dispatcher.executeAndSyncWithGUI(new Runnable() {
						@Override
						public void run() {
							Item item = mainView.getJarListView().getItem(url);
							if (item != null) {
								item.getProgressBar().setProgress((float) progress);
							}
						}
					});
				}
				@Override
				public void finished() {
				}
			});
		} catch (InvalidURIException e) {
			e.printStackTrace();
		}
	}

}
